package com.pashanhoo.designpatterns.proxy.dynamic.cglibproxy.yunali;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 胡庆安
 * @version V1.0
 * @Package com.pashanhoo.designpatterns.proxy.dynamic.cglibproxy.yunali
 * @date 2020/11/17 13:25
 * @Copyright © 2010-2020 爬山虎科技（扬州）股份有限公司
 */
public class ProxyHelp implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("after");
        return result;
    }

    public static Object getProxy(Class<?> cls){
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(new ProxyHelp());
        enhancer.setSuperclass(cls);
        return enhancer.create();
    }

}
